package com.rm.zeyou.util.sms.impl;


import com.rm.zeyou.constant.CommonConstants;
import com.rm.zeyou.constant.ProfileConstants;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.SmsTemplateConfigMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.util.check.ValitedPhone;
import com.rm.zeyou.util.obtainprofile.ObtainProfileUtil;
import com.rm.zeyou.util.sms.CreateSmsCode;
import com.rm.zeyou.util.sms.SendSmsService;
import com.rm.zeyou.util.sms.SmsSample;
import com.rm.zeyou.util.sms.SmsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Log4j2
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SmsServiceImpl implements SmsService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource(name = "aliyunSendSms")
    private SendSmsService sendSmsService;

    @Resource
    private SmsSample smsSample;
    @Resource
    private ObtainProfileUtil obtainProfileUtil;
    @Resource
    private SmsTemplateConfigMapper smsTemplateConfigMapper;

    /**
     * 阿里云发送验证码
     *
     * @param tel
     * @return
     */
    @Override
    public ApiRestResponse sendCode(String tel, String type) throws Exception {
        if (!ValitedPhone.isValidPhoneNumber(tel)) {
            return ApiRestResponse.error(ErrorCodeExceptionEnum.PHONE_NUMBER_ERROR);
        }
        // 测试发送
        // String smsResult = CodeUtil.creatCode();
        // 不存在 设置key 存在 验证value
        Boolean aBoolean = stringRedisTemplate.hasKey(type + tel);
        log.info("aBoolean:{}", aBoolean);
        log.info("tel:{},type:{},type + tel:{}", tel, type, type + tel);
        if (Boolean.FALSE.equals(aBoolean)) {
            /*
             * 判断环境进行发送验证码
             * dev(本地环境不发送验证码)
             */
            if (obtainProfileUtil.getActiveProfile().equalsIgnoreCase(ProfileConstants.PROFILE_DEV)) {
                String smsResult = "888888";
                log.info("本地环境获取验证码,手机号:{},验证码:{}", tel, smsResult);
                stringRedisTemplate.opsForValue().set(type + tel, smsResult, CommonConstants.SMS_INVALID_CODE_TIME, TimeUnit.SECONDS);
                return ApiRestResponse.success(smsResult);
            } else {
                String code = CreateSmsCode.creatCode();
                String templateCode = smsTemplateConfigMapper.selectOneData("captcha");
                Integer b = sendSmsService.sendSms(tel, code, templateCode);
                log.info("线上环境获取验证码,手机号:{},验证码:{},发送状态:{}", tel, code, b);
                stringRedisTemplate.opsForValue().set(type + tel, code, CommonConstants.SMS_INVALID_CODE_TIME, TimeUnit.SECONDS);
                return ApiRestResponse.success();
            }
        } else {
            Long timeNum = stringRedisTemplate.getExpire(type + tel);
            return ApiRestResponse.error("506", "验证码获取失败,请避免重复获取,还剩" + timeNum + "秒");
        }
    }

    /**
     * 短信宝发送
     *
     * @param tel
     * @param type
     * @return
     * @throws Exception
     */
    @Override
    public ApiRestResponse sendCodeD(String tel, String type) throws Exception {
        //if (!ValitedPhone.isValidPhoneNumber(tel)) {
        //    return ApiRestResponse.error(ErrorCodeExceptionEnum.PHONE_NUMBER_ERROR);
        //}
        // 测试发送
        // String smsResult = CodeUtil.creatCode();
        // 不存在 设置key 存在 验证value
        Boolean aBoolean = stringRedisTemplate.hasKey(type + tel);
        log.info("验证码缓存状态 Boolean:{}", aBoolean);
        log.info("tel:{},type:{},type + tel:{}", tel, type, type + tel);
        if (Boolean.FALSE.equals(aBoolean)) {
            /*
             * 判断环境进行发送验证码
             * dev(本地环境不发送验证码)
             */
            if (obtainProfileUtil.getActiveProfile().equalsIgnoreCase(ProfileConstants.PROFILE_DEV)) {
                String smsResult = "888888";
                log.info("本地环境获取验证码,手机号:{},验证码:{}", tel, smsResult);
                stringRedisTemplate.opsForValue().set(type + tel, smsResult, CommonConstants.SMS_INVALID_CODE_TIME, TimeUnit.SECONDS);
                return ApiRestResponse.success(smsResult);
            } else {
                String code = CreateSmsCode.creatCode();
                String b = smsSample.sendSms(tel, code, type);
                log.info("线上环境获取验证码,手机号:{},验证码:{},发送状态:{}", tel, code, b);
                stringRedisTemplate.opsForValue().set(type + tel, code, CommonConstants.SMS_INVALID_CODE_TIME, TimeUnit.SECONDS);
                return ApiRestResponse.success();
            }
        } else {
            Long timeNum = stringRedisTemplate.getExpire(type + tel);
            return ApiRestResponse.error("506", "验证码获取失败,请避免重复获取,还剩" + timeNum + "秒");
        }
    }

    /**
     * 发送订单通知给技师
     *
     * @param tel
     * @return
     */
    @Override
    public ApiRestResponse orderSendSms(String tel, String name,String templateCode) throws Exception {
        if (!ValitedPhone.isValidPhoneNumber(tel)) {
            return ApiRestResponse.error(ErrorCodeExceptionEnum.PHONE_NUMBER_ERROR);
        }
        Integer b = sendSmsService.orderSendSms(tel, name, templateCode);
        log.info("线上环境获取验证码,手机号:{},发送状态:{}", tel,  b);
        return ApiRestResponse.success();
    }

}
